package backend;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import beans.CharBean;
import beans.ProbenBean;

public class ProbenWuerfeln {

	class buttonListender implements ActionListener {

		public void actionPerformed(final ActionEvent e) {

			JButton button = (JButton) e.getSource(); // Den Button aus dem
			// actionevent auslesen
			JFrame frame2 = (JFrame) button.getRootPane().getParent();
			// über den Buttone den frame holen und verschwinden lassen.
			frame2.setVisible(false);
		}
	}

	private Daten daten = null;
	private Random random = new Random();
	private JButton schliessenButton;
	private JFrame frame;

	public ProbenWuerfeln(Daten daten) {

		this.daten = daten;
	}

	public int rollTheDice(int maxErgebnis) {

		int ergebnis = 0;
		ergebnis = random.nextInt(maxErgebnis);
		ergebnis++;// nötig da es sonst ergebnisse von 0- bis maxergebnis geben
		// würde
		return ergebnis;
	}

	public void wuerfeln(int charId, int probenId) {

		JPanel mainPanel2 = new JPanel(new BorderLayout());
		JPanel mainPanel = new JPanel();
		JLabel labelToAdd = new JLabel();
		CharBean chara = daten.getCharById(charId);
		ProbenBean probe = daten.getProbeById(probenId);
		int wert1 = chara.getAttributByName(probe.getAtt1());
		int wert2 = chara.getAttributByName(probe.getAtt2());
		int wert3 = chara.getAttributByName(probe.getAtt3());
		int taw = chara.getTawById(probenId);
		int taw2 = taw;
		int dice1 = rollTheDice(20);
		int dice2 = rollTheDice(20);
		int dice3 = rollTheDice(20);
		int erg1 = 0;
		int erg2 = 0;
		int erg3 = 0;
		if (wert1 < dice1) {
			erg1 = dice1 - wert1;
		}
		if (wert2 < dice2) {
			erg2 = dice2 - wert2;
		}
		if (wert3 < dice3) {
			erg3 = dice3 - wert3;
		}
		taw = taw - erg1 - erg2 - erg3;
		mainPanel.setLayout(new GridLayout(8, 0, 5, 5));
		labelToAdd = new JLabel("Name: " + chara.getName());
		mainPanel.add(labelToAdd);
		labelToAdd = new JLabel("Probe: " + probe.getName());
		mainPanel.add(labelToAdd);
		labelToAdd = new JLabel("TaW: " + taw + "/" + taw2);
		mainPanel.add(labelToAdd);
		labelToAdd = new JLabel(probe.getAtt1() + ": " + dice1 + "/" + wert1);
		mainPanel.add(labelToAdd);
		labelToAdd = new JLabel(probe.getAtt2() + ": " + dice2 + "/" + wert2);
		mainPanel.add(labelToAdd);
		labelToAdd = new JLabel(probe.getAtt3() + ": " + dice3 + "/" + wert3);
		mainPanel.add(labelToAdd);
		if ((dice1 == 20 && dice2 == 20) || (dice1 == 20 && dice3 == 20)
					|| (dice2 == 20 && dice3 == 20)) {
			labelToAdd = new JLabel("Patzer!");
			Font font = new Font("Calibri", Font.BOLD, 18);
			labelToAdd.setForeground(Color.RED);
			labelToAdd.setFont(font);
			labelToAdd.setBackground(Color.BLACK);
			mainPanel.add(labelToAdd);
		} else if ((dice1 == 1 && dice2 == 1) || (dice1 == 1 && dice2 == 1)
					|| (dice1 == 1 && dice2 == 1)) {
			labelToAdd = new JLabel("Meister!");
			Font font = new Font("Calibri", Font.BOLD, 18);
			labelToAdd.setFont(font);
			labelToAdd.setForeground(Color.GREEN);
			labelToAdd.setBackground(Color.BLACK);
			mainPanel.add(labelToAdd);
		} else {
			if (taw >= 0) {
				labelToAdd = new JLabel("Bestanden!");
				labelToAdd.setForeground(Color.GREEN);
				mainPanel.add(labelToAdd);
			} else {
				labelToAdd = new JLabel("Versagt!");
				labelToAdd.setForeground(Color.RED);
				labelToAdd.setBackground(Color.BLACK);
				mainPanel.add(labelToAdd);
			}
		}
		daten.addToVerlauf("Würfelergebnis", chara.getName() + " "
					+ probe.getName() + " " + probe.getAtt1() + ": " + dice1
					+ "/" + wert1 + " " + probe.getAtt2() + ": " + dice2 + "/"
					+ wert2 + " " + probe.getAtt3() + ": " + dice3 + "/"
					+ wert3);
		frame = new JFrame();
		frame.setTitle(chara.getName());
		schliessenButton = new JButton("schliessen");
		schliessenButton.addActionListener(new buttonListender());
		mainPanel2.add(mainPanel, BorderLayout.CENTER);
		mainPanel2.add(schliessenButton, BorderLayout.SOUTH);
		frame.getContentPane().add(mainPanel2);
		frame.setLocation(150, 100);
		frame.setSize(200, 300);
		frame.setVisible(true);
	}
}